package gientech.spring.demo.util;

import com.alibaba.fastjson.JSONObject;
import gientech.spring.demo.dto.UserChangeEvent;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class UserMessageProducer {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    public void sendUserChangeEvent(UserChangeEvent userChangeEvent) {
        // 1.构造消息
        JSONObject object = new JSONObject();
        object.put("userChange", userChangeEvent);
        Message<String> msg = MessageBuilder.withPayload(object.toJSONString()).build();
        // 2.发送消息
        rocketMQTemplate.sendMessageInTransaction("producer_ensure_transfer",
                "topic_ensure_transfer",
                msg, null);
    }
}
